package controls;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pages.WebPage;
import reports.Report;
import utils.Events;

public class Button {
	private WebElement button;
	private By by;
	private Events events;

	
	public Button(WebElement buttonName, String buttonDesc) {
		button = buttonName;
		WebPage.elementList.put(button, buttonDesc);
	}
	
	
	public Button(String buttonID,String buttonDesc,ElementFinder util,Events events){
		this.events=events;
		if(buttonID.startsWith("name")){
			button=util.findElementByName(buttonID);
		}
		else if(buttonID.startsWith("css")){
			button=util.findElementByCss(buttonID);
		}
		else if(buttonID.startsWith("//")){
			button=util.findElementByXpath(buttonID);
		}
		else if(buttonID.startsWith("id")){
			button=util.findElementByID(buttonID);
		}
		else{
			Report.log("button is not found");
		}
		WebPage.elementList.put(button, buttonDesc);
	}
	
	
	public Button(String buttonID,By byOfButton, String buttonDesc,ElementFinder util,Events events){
		this.events=events;
		if(buttonID.startsWith("name")){
			button=util.findElementByName(buttonID);
		}
		else if(buttonID.startsWith("css")){
			button=util.findElementByCss(buttonID);
		}
		else if(buttonID.startsWith("//")){
			button=util.findElementByXpath(buttonID);
		}
		else if(buttonID.startsWith("id")){
			button=util.findElementByID(buttonID);
		}
		else{
			Report.log("button is not found");
		}
		by=byOfButton;
		WebPage.elementList.put(button, buttonDesc);
	}
	
	
	/**
	 * Constructor for button when By of the button is required
	 * @author Pradeep Sundaram
	 * @param buttonName
	 * @param byOfButton
	 * @param buttonDesc
	 */
	public Button(WebElement buttonName, By byOfButton, String buttonDesc) {
		by=byOfButton;
		button = buttonName;
		WebPage.elementList.put(button, buttonDesc);
	}
	
	/**
	 * This method will return the By of the text field
	 * @author Pradeep Sundaram
	 * @return By
	 */
	public By getBy(){
		return by;
	}
	
	/**
	 * This method will click in the button
	 * 
	 * @author Pradeep Sundaram
	 */
	public void click() {
		events.click(button);
	}
	
	/**
	 * this method will double click on the button
	 * 
	 * @author PSubramani33 
	 */
	public void doubleClick(){
		events.doubleClick(button);
	}
	
	/**
	 * This method will return the webElement of the button
	 * @author Pradeep Sundaram
	 * @return WebElement
	 */
	public WebElement getWebElement() {
		return button;
	}
	
	
	/**
	 * will return boolean based on the presence of the button
	 * 
	 * @author Pradeep Sundaram
	 * @return boolean
	 */
	public boolean isDisplayed() {
		Report.log("Checking whether the field " + WebPage.elementList.get(button)+" is displayed.<BR>");
		return button.isDisplayed();
	}
	
	/**
	 * This method will return the tool tip of the button
	 * 
	 * @author PSubramani33
	 * @return String
	 */
	public String getToolTip(){
		Report.log("Getting the tool tip of the button " + WebPage.elementList.get(button)+".<BR>");
		return button.getAttribute("title");
	}
	
	private void testMethod(){
		
	}
}
